<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>Class</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <link rel="stylesheet" href="../css/reset.css" type="text/css" media="screen" />
<link rel="stylesheet" href="../css/main.css" type="text/css" media="screen" />
<link rel="stylesheet" href="../css/github.css" type="text/css" media="screen" />
<script src="../js/jquery-1.3.2.min.js" type="text/javascript" charset="utf-8"></script>
<script src="../js/jquery-effect.js" type="text/javascript" charset="utf-8"></script>
<script src="../js/main.js" type="text/javascript" charset="utf-8"></script>
<script src="../js/highlight.pack.js" type="text/javascript" charset="utf-8"></script>

</head>

<body>     
    <div class="banner">
        
            <span>Ruby on Rails v4.0.0</span><br />
        
        <h1>
            <span class="type">Class</span> 
            Class 
            
                <span class="parent">&lt; 
                    
                    <a href="Object.html">Object</a>
                    
                </span>
            
        </h1>
        <ul class="files">
            
            <li><a href="../files/activesupport/lib/active_support/core_ext/class/attribute_rb.html">activesupport/lib/active_support/core_ext/class/attribute.rb</a></li>
            
            <li><a href="../files/activesupport/lib/active_support/core_ext/class/attribute_accessors_rb.html">activesupport/lib/active_support/core_ext/class/attribute_accessors.rb</a></li>
            
            <li><a href="../files/activesupport/lib/active_support/core_ext/class/delegating_attributes_rb.html">activesupport/lib/active_support/core_ext/class/delegating_attributes.rb</a></li>
            
            <li><a href="../files/activesupport/lib/active_support/core_ext/class/subclasses_rb.html">activesupport/lib/active_support/core_ext/class/subclasses.rb</a></li>
            
        </ul>
    </div>
    <div id="bodyContent">
        <div id="content">
  
    <div class="description">
      
<p>Extends the class object with class and instance accessors for class
attributes, just like the native attr* accessors for instance attributes.</p>

    </div>
  


  


  
  


  


  
    <!-- Method ref -->
    <div class="sectiontitle">Methods</div>
    <dl class="methods">
      
        <dt>C</dt>
        <dd>
          <ul>
            
              
              <li>
                <a href="Class.html#method-i-cattr_accessor">cattr_accessor</a>,
              </li>
            
              
              <li>
                <a href="Class.html#method-i-cattr_reader">cattr_reader</a>,
              </li>
            
              
              <li>
                <a href="Class.html#method-i-cattr_writer">cattr_writer</a>,
              </li>
            
              
              <li>
                <a href="Class.html#method-i-class_attribute">class_attribute</a>
              </li>
            
          </ul>
        </dd>
      
        <dt>S</dt>
        <dd>
          <ul>
            
              
              <li>
                <a href="Class.html#method-i-subclasses">subclasses</a>,
              </li>
            
              
              <li>
                <a href="Class.html#method-i-superclass_delegating_accessor">superclass_delegating_accessor</a>
              </li>
            
          </ul>
        </dd>
      
    </dl>
  

  



  

    

    

    


    


    <!-- Methods -->
        
      <div class="sectiontitle">Instance Public methods</div>
      
        <div class="method">
          <div class="title method-title" id="method-i-cattr_accessor">
            
              <b>cattr_accessor</b>(*syms, &amp;blk)
            
            <a href="Class.html#method-i-cattr_accessor" name="method-i-cattr_accessor" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              <p>Defines both class and instance accessors for class attributes.</p>

<pre class="ruby"><span class="ruby-keyword">class</span> <span class="ruby-constant">Person</span>
  <span class="ruby-identifier">cattr_accessor</span> :<span class="ruby-identifier">hair_colors</span>
<span class="ruby-keyword">end</span>

<span class="ruby-constant">Person</span>.<span class="ruby-identifier">hair_colors</span> = [:<span class="ruby-identifier">brown</span>, :<span class="ruby-identifier">black</span>, :<span class="ruby-identifier">blonde</span>, :<span class="ruby-identifier">red</span>]
<span class="ruby-constant">Person</span>.<span class="ruby-identifier">hair_colors</span>     <span class="ruby-comment"># =&gt; [:brown, :black, :blonde, :red]</span>
<span class="ruby-constant">Person</span>.<span class="ruby-identifier">new</span>.<span class="ruby-identifier">hair_colors</span> <span class="ruby-comment"># =&gt; [:brown, :black, :blonde, :red]</span>
</pre>

<p>If a subclass changes the value then that would also change the value for
parent class. Similarly if parent class changes the value then that would
change the value of subclasses too.</p>

<pre class="ruby"><span class="ruby-keyword">class</span> <span class="ruby-constant">Male</span> <span class="ruby-operator">&lt;</span> <span class="ruby-constant">Person</span>
<span class="ruby-keyword">end</span>

<span class="ruby-constant">Male</span>.<span class="ruby-identifier">hair_colors</span> <span class="ruby-operator">&lt;&lt;</span> :<span class="ruby-identifier">blue</span>
<span class="ruby-constant">Person</span>.<span class="ruby-identifier">hair_colors</span> <span class="ruby-comment"># =&gt; [:brown, :black, :blonde, :red, :blue]</span>
</pre>

<p>To opt out of the instance writer method, pass <code>instance_writer:
false</code>. To opt out of the instance reader method, pass
<code>instance_reader: false</code>.</p>

<pre class="ruby"><span class="ruby-keyword">class</span> <span class="ruby-constant">Person</span>
  <span class="ruby-identifier">cattr_accessor</span> :<span class="ruby-identifier">hair_colors</span>, <span class="ruby-identifier">instance_writer</span><span class="ruby-operator">:</span> <span class="ruby-keyword">false</span>, <span class="ruby-identifier">instance_reader</span><span class="ruby-operator">:</span> <span class="ruby-keyword">false</span>
<span class="ruby-keyword">end</span>

<span class="ruby-constant">Person</span>.<span class="ruby-identifier">new</span>.<span class="ruby-identifier">hair_colors</span> = [:<span class="ruby-identifier">brown</span>]  <span class="ruby-comment"># =&gt; NoMethodError</span>
<span class="ruby-constant">Person</span>.<span class="ruby-identifier">new</span>.<span class="ruby-identifier">hair_colors</span>             <span class="ruby-comment"># =&gt; NoMethodError</span>
</pre>

<p>Or pass <code>instance_accessor: false</code>, to opt out both instance
methods.</p>

<pre class="ruby"><span class="ruby-keyword">class</span> <span class="ruby-constant">Person</span>
  <span class="ruby-identifier">cattr_accessor</span> :<span class="ruby-identifier">hair_colors</span>, <span class="ruby-identifier">instance_accessor</span><span class="ruby-operator">:</span> <span class="ruby-keyword">false</span>
<span class="ruby-keyword">end</span>

<span class="ruby-constant">Person</span>.<span class="ruby-identifier">new</span>.<span class="ruby-identifier">hair_colors</span> = [:<span class="ruby-identifier">brown</span>]  <span class="ruby-comment"># =&gt; NoMethodError</span>
<span class="ruby-constant">Person</span>.<span class="ruby-identifier">new</span>.<span class="ruby-identifier">hair_colors</span>             <span class="ruby-comment"># =&gt; NoMethodError</span>
</pre>

<p>Also you can pass a block to set up the attribute with a default value.</p>

<pre class="ruby"><span class="ruby-keyword">class</span> <span class="ruby-constant">Person</span>
  <span class="ruby-identifier">cattr_accessor</span> :<span class="ruby-identifier">hair_colors</span> <span class="ruby-keyword">do</span>
    [:<span class="ruby-identifier">brown</span>, :<span class="ruby-identifier">black</span>, :<span class="ruby-identifier">blonde</span>, :<span class="ruby-identifier">red</span>]
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span>

<span class="ruby-constant">Person</span>.<span class="ruby-identifier">class_variable_get</span>(<span class="ruby-string">&quot;@@hair_colors&quot;</span>) <span class="ruby-comment">#=&gt; [:brown, :black, :blonde, :red]</span>
</pre>
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-i-cattr_accessor_source')" id="l_method-i-cattr_accessor_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/4037fc83c1ad6655569d630e2c909d846a607705/activesupport/lib/active_support/core_ext/class/attribute_accessors.rb#L166" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-i-cattr_accessor_source" class="dyn-source">
                <pre><span class="ruby-comment"># File activesupport/lib/active_support/core_ext/class/attribute_accessors.rb, line 166</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">cattr_accessor</span>(*<span class="ruby-identifier">syms</span>, &amp;<span class="ruby-identifier">blk</span>)
  <span class="ruby-identifier">cattr_reader</span>(*<span class="ruby-identifier">syms</span>)
  <span class="ruby-identifier">cattr_writer</span>(*<span class="ruby-identifier">syms</span>, &amp;<span class="ruby-identifier">blk</span>)
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
        
        <div class="method">
          <div class="title method-title" id="method-i-cattr_reader">
            
              <b>cattr_reader</b>(*syms)
            
            <a href="Class.html#method-i-cattr_reader" name="method-i-cattr_reader" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              <p>Defines a class attribute if it’s not defined and creates a reader method
that returns the attribute value.</p>

<pre class="ruby"><span class="ruby-keyword">class</span> <span class="ruby-constant">Person</span>
  <span class="ruby-identifier">cattr_reader</span> :<span class="ruby-identifier">hair_colors</span>
<span class="ruby-keyword">end</span>

<span class="ruby-constant">Person</span>.<span class="ruby-identifier">class_variable_set</span>(<span class="ruby-string">&quot;@@hair_colors&quot;</span>, [:<span class="ruby-identifier">brown</span>, :<span class="ruby-identifier">black</span>])
<span class="ruby-constant">Person</span>.<span class="ruby-identifier">hair_colors</span>     <span class="ruby-comment"># =&gt; [:brown, :black]</span>
<span class="ruby-constant">Person</span>.<span class="ruby-identifier">new</span>.<span class="ruby-identifier">hair_colors</span> <span class="ruby-comment"># =&gt; [:brown, :black]</span>
</pre>

<p>The attribute name must be a valid method name in Ruby.</p>

<pre class="ruby"><span class="ruby-keyword">class</span> <span class="ruby-constant">Person</span>
  <span class="ruby-identifier">cattr_reader</span> :<span class="ruby-string">&quot;1_Badname &quot;</span>
<span class="ruby-keyword">end</span>
<span class="ruby-comment"># =&gt; NameError: invalid attribute name</span>
</pre>

<p>If you want to opt out the instance reader method, you can pass
<code>instance_reader: false</code> or <code>instance_accessor:
false</code>.</p>

<pre class="ruby"><span class="ruby-keyword">class</span> <span class="ruby-constant">Person</span>
  <span class="ruby-identifier">cattr_reader</span> :<span class="ruby-identifier">hair_colors</span>, <span class="ruby-identifier">instance_reader</span><span class="ruby-operator">:</span> <span class="ruby-keyword">false</span>
<span class="ruby-keyword">end</span>

<span class="ruby-constant">Person</span>.<span class="ruby-identifier">new</span>.<span class="ruby-identifier">hair_colors</span> <span class="ruby-comment"># =&gt; NoMethodError</span>
</pre>
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-i-cattr_reader_source')" id="l_method-i-cattr_reader_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/4037fc83c1ad6655569d630e2c909d846a607705/activesupport/lib/active_support/core_ext/class/attribute_accessors.rb#L32" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-i-cattr_reader_source" class="dyn-source">
                <pre><span class="ruby-comment"># File activesupport/lib/active_support/core_ext/class/attribute_accessors.rb, line 32</span>
  <span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">cattr_reader</span>(*<span class="ruby-identifier">syms</span>)
    <span class="ruby-identifier">options</span> = <span class="ruby-identifier">syms</span>.<span class="ruby-identifier">extract_options!</span>
    <span class="ruby-identifier">syms</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">sym</span><span class="ruby-operator">|</span>
      <span class="ruby-identifier">raise</span> <span class="ruby-constant">NameError</span>.<span class="ruby-identifier">new</span>(<span class="ruby-node">&quot;invalid class attribute name: #{sym}&quot;</span>) <span class="ruby-keyword">unless</span> <span class="ruby-identifier">sym</span> <span class="ruby-operator">=~</span> <span class="ruby-regexp">/^[_A-Za-z]\w*$/</span>
      <span class="ruby-identifier">class_eval</span>(<span class="ruby-string">&quot;        unless defined? @@#{sym}
          @@#{sym} = nil
        end

        def self.#{sym}
          @@#{sym}
        end
&quot;</span>, <span class="ruby-keyword">__FILE__</span>, <span class="ruby-keyword">__LINE__</span> <span class="ruby-operator">+</span> <span class="ruby-number">1</span>)

      <span class="ruby-keyword">unless</span> <span class="ruby-identifier">options</span>[<span class="ruby-value">:instance_reader</span>] <span class="ruby-operator">==</span> <span class="ruby-keyword">false</span> <span class="ruby-operator">||</span> <span class="ruby-identifier">options</span>[<span class="ruby-value">:instance_accessor</span>] <span class="ruby-operator">==</span> <span class="ruby-keyword">false</span>
        <span class="ruby-identifier">class_eval</span>(<span class="ruby-string">&quot;          def #{sym}
            @@#{sym}
          end
&quot;</span>, <span class="ruby-keyword">__FILE__</span>, <span class="ruby-keyword">__LINE__</span> <span class="ruby-operator">+</span> <span class="ruby-number">1</span>)
      <span class="ruby-keyword">end</span>
    <span class="ruby-keyword">end</span>
  <span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
        
        <div class="method">
          <div class="title method-title" id="method-i-cattr_writer">
            
              <b>cattr_writer</b>(*syms)
            
            <a href="Class.html#method-i-cattr_writer" name="method-i-cattr_writer" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              <p>Defines a class attribute if it’s not defined and creates a writer method
to allow assignment to the attribute.</p>

<pre class="ruby"><span class="ruby-keyword">class</span> <span class="ruby-constant">Person</span>
  <span class="ruby-identifier">cattr_writer</span> :<span class="ruby-identifier">hair_colors</span>
<span class="ruby-keyword">end</span>

<span class="ruby-constant">Person</span>.<span class="ruby-identifier">hair_colors</span> = [:<span class="ruby-identifier">brown</span>, :<span class="ruby-identifier">black</span>]
<span class="ruby-constant">Person</span>.<span class="ruby-identifier">class_variable_get</span>(<span class="ruby-string">&quot;@@hair_colors&quot;</span>) <span class="ruby-comment"># =&gt; [:brown, :black]</span>
<span class="ruby-constant">Person</span>.<span class="ruby-identifier">new</span>.<span class="ruby-identifier">hair_colors</span> = [:<span class="ruby-identifier">blonde</span>, :<span class="ruby-identifier">red</span>]
<span class="ruby-constant">Person</span>.<span class="ruby-identifier">class_variable_get</span>(<span class="ruby-string">&quot;@@hair_colors&quot;</span>) <span class="ruby-comment"># =&gt; [:blonde, :red]</span>
</pre>

<p>The attribute name must be a valid method name in Ruby.</p>

<pre class="ruby"><span class="ruby-keyword">class</span> <span class="ruby-constant">Person</span>
  <span class="ruby-identifier">cattr_writer</span> :<span class="ruby-string">&quot;1_Badname &quot;</span>
<span class="ruby-keyword">end</span>
<span class="ruby-comment"># =&gt; NameError: invalid attribute name</span>
</pre>

<p>If you want to opt out the instance writer method, pass
<code>instance_writer: false</code> or <code>instance_accessor:
false</code>.</p>

<pre class="ruby"><span class="ruby-keyword">class</span> <span class="ruby-constant">Person</span>
  <span class="ruby-identifier">cattr_writer</span> :<span class="ruby-identifier">hair_colors</span>, <span class="ruby-identifier">instance_writer</span><span class="ruby-operator">:</span> <span class="ruby-keyword">false</span>
<span class="ruby-keyword">end</span>

<span class="ruby-constant">Person</span>.<span class="ruby-identifier">new</span>.<span class="ruby-identifier">hair_colors</span> = [:<span class="ruby-identifier">blonde</span>, :<span class="ruby-identifier">red</span>] <span class="ruby-comment"># =&gt; NoMethodError</span>
</pre>

<p>Also, you can pass a block to set up the attribute with a default value.</p>

<pre class="ruby"><span class="ruby-keyword">class</span> <span class="ruby-constant">Person</span>
  <span class="ruby-identifier">cattr_writer</span> :<span class="ruby-identifier">hair_colors</span> <span class="ruby-keyword">do</span>
    [:<span class="ruby-identifier">brown</span>, :<span class="ruby-identifier">black</span>, :<span class="ruby-identifier">blonde</span>, :<span class="ruby-identifier">red</span>]
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span>

<span class="ruby-constant">Person</span>.<span class="ruby-identifier">class_variable_get</span>(<span class="ruby-string">&quot;@@hair_colors&quot;</span>) <span class="ruby-comment"># =&gt; [:brown, :black, :blonde, :red]</span>
</pre>
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-i-cattr_writer_source')" id="l_method-i-cattr_writer_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/4037fc83c1ad6655569d630e2c909d846a607705/activesupport/lib/active_support/core_ext/class/attribute_accessors.rb#L93" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-i-cattr_writer_source" class="dyn-source">
                <pre><span class="ruby-comment"># File activesupport/lib/active_support/core_ext/class/attribute_accessors.rb, line 93</span>
  <span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">cattr_writer</span>(*<span class="ruby-identifier">syms</span>)
    <span class="ruby-identifier">options</span> = <span class="ruby-identifier">syms</span>.<span class="ruby-identifier">extract_options!</span>
    <span class="ruby-identifier">syms</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">sym</span><span class="ruby-operator">|</span>
      <span class="ruby-identifier">raise</span> <span class="ruby-constant">NameError</span>.<span class="ruby-identifier">new</span>(<span class="ruby-node">&quot;invalid class attribute name: #{sym}&quot;</span>) <span class="ruby-keyword">unless</span> <span class="ruby-identifier">sym</span> <span class="ruby-operator">=~</span> <span class="ruby-regexp">/^[_A-Za-z]\w*$/</span>
      <span class="ruby-identifier">class_eval</span>(<span class="ruby-string">&quot;        unless defined? @@#{sym}
          @@#{sym} = nil
        end

        def self.#{sym}=(obj)
          @@#{sym} = obj
        end
&quot;</span>, <span class="ruby-keyword">__FILE__</span>, <span class="ruby-keyword">__LINE__</span> <span class="ruby-operator">+</span> <span class="ruby-number">1</span>)

      <span class="ruby-keyword">unless</span> <span class="ruby-identifier">options</span>[<span class="ruby-value">:instance_writer</span>] <span class="ruby-operator">==</span> <span class="ruby-keyword">false</span> <span class="ruby-operator">||</span> <span class="ruby-identifier">options</span>[<span class="ruby-value">:instance_accessor</span>] <span class="ruby-operator">==</span> <span class="ruby-keyword">false</span>
        <span class="ruby-identifier">class_eval</span>(<span class="ruby-string">&quot;          def #{sym}=(obj)
            @@#{sym} = obj
          end
&quot;</span>, <span class="ruby-keyword">__FILE__</span>, <span class="ruby-keyword">__LINE__</span> <span class="ruby-operator">+</span> <span class="ruby-number">1</span>)
      <span class="ruby-keyword">end</span>
      <span class="ruby-identifier">send</span>(<span class="ruby-node">&quot;#{sym}=&quot;</span>, <span class="ruby-keyword">yield</span>) <span class="ruby-keyword">if</span> <span class="ruby-identifier">block_given?</span>
    <span class="ruby-keyword">end</span>
  <span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
        
        <div class="method">
          <div class="title method-title" id="method-i-class_attribute">
            
              <b>class_attribute</b>(*attrs)
            
            <a href="Class.html#method-i-class_attribute" name="method-i-class_attribute" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              <p>Declare a class-level attribute whose value is inheritable by subclasses.
Subclasses can change their own value and it will not impact parent class.</p>

<pre class="ruby"><span class="ruby-keyword">class</span> <span class="ruby-constant">Base</span>
  <span class="ruby-identifier">class_attribute</span> :<span class="ruby-identifier">setting</span>
<span class="ruby-keyword">end</span>

<span class="ruby-keyword">class</span> <span class="ruby-constant">Subclass</span> <span class="ruby-operator">&lt;</span> <span class="ruby-constant">Base</span>
<span class="ruby-keyword">end</span>

<span class="ruby-constant">Base</span>.<span class="ruby-identifier">setting</span> = <span class="ruby-keyword">true</span>
<span class="ruby-constant">Subclass</span>.<span class="ruby-identifier">setting</span>            <span class="ruby-comment"># =&gt; true</span>
<span class="ruby-constant">Subclass</span>.<span class="ruby-identifier">setting</span> = <span class="ruby-keyword">false</span>
<span class="ruby-constant">Subclass</span>.<span class="ruby-identifier">setting</span>            <span class="ruby-comment"># =&gt; false</span>
<span class="ruby-constant">Base</span>.<span class="ruby-identifier">setting</span>                <span class="ruby-comment"># =&gt; true</span>
</pre>

<p>In the above case as long as Subclass does not assign a value to setting by
performing <code>Subclass.setting = <em>something</em> </code>,
<code>Subclass.setting</code> would read value assigned to parent class.
Once Subclass assigns a value then the value assigned by Subclass would be
returned.</p>

<p>This matches normal Ruby method inheritance: think of writing an attribute
on a subclass as overriding the reader method. However, you need to be
aware when using <code>class_attribute</code> with mutable structures as
<code>Array</code> or <code>Hash</code>. In such cases, you don’t want to
do changes in places but use setters:</p>

<pre class="ruby"><span class="ruby-constant">Base</span>.<span class="ruby-identifier">setting</span> = []
<span class="ruby-constant">Base</span>.<span class="ruby-identifier">setting</span>                <span class="ruby-comment"># =&gt; []</span>
<span class="ruby-constant">Subclass</span>.<span class="ruby-identifier">setting</span>            <span class="ruby-comment"># =&gt; []</span>

<span class="ruby-comment"># Appending in child changes both parent and child because it is the same object:</span>
<span class="ruby-constant">Subclass</span>.<span class="ruby-identifier">setting</span> <span class="ruby-operator">&lt;&lt;</span> :<span class="ruby-identifier">foo</span>
<span class="ruby-constant">Base</span>.<span class="ruby-identifier">setting</span>               <span class="ruby-comment"># =&gt; [:foo]</span>
<span class="ruby-constant">Subclass</span>.<span class="ruby-identifier">setting</span>           <span class="ruby-comment"># =&gt; [:foo]</span>

<span class="ruby-comment"># Use setters to not propagate changes:</span>
<span class="ruby-constant">Base</span>.<span class="ruby-identifier">setting</span> = []
<span class="ruby-constant">Subclass</span>.<span class="ruby-identifier">setting</span> <span class="ruby-operator">+=</span> [:<span class="ruby-identifier">foo</span>]
<span class="ruby-constant">Base</span>.<span class="ruby-identifier">setting</span>               <span class="ruby-comment"># =&gt; []</span>
<span class="ruby-constant">Subclass</span>.<span class="ruby-identifier">setting</span>           <span class="ruby-comment"># =&gt; [:foo]</span>
</pre>

<p>For convenience, an instance predicate method is defined as well. To skip
it, pass <code>instance_predicate: false</code>.</p>

<pre class="ruby"><span class="ruby-constant">Subclass</span>.<span class="ruby-identifier">setting?</span>       <span class="ruby-comment"># =&gt; false</span>
</pre>

<p>Instances may overwrite the class value in the same way:</p>

<pre class="ruby"><span class="ruby-constant">Base</span>.<span class="ruby-identifier">setting</span> = <span class="ruby-keyword">true</span>
<span class="ruby-identifier">object</span> = <span class="ruby-constant">Base</span>.<span class="ruby-identifier">new</span>
<span class="ruby-identifier">object</span>.<span class="ruby-identifier">setting</span>          <span class="ruby-comment"># =&gt; true</span>
<span class="ruby-identifier">object</span>.<span class="ruby-identifier">setting</span> = <span class="ruby-keyword">false</span>
<span class="ruby-identifier">object</span>.<span class="ruby-identifier">setting</span>          <span class="ruby-comment"># =&gt; false</span>
<span class="ruby-constant">Base</span>.<span class="ruby-identifier">setting</span>            <span class="ruby-comment"># =&gt; true</span>
</pre>

<p>To opt out of the instance reader method, pass <code>instance_reader:
false</code>.</p>

<pre class="ruby"><span class="ruby-identifier">object</span>.<span class="ruby-identifier">setting</span>          <span class="ruby-comment"># =&gt; NoMethodError</span>
<span class="ruby-identifier">object</span>.<span class="ruby-identifier">setting?</span>         <span class="ruby-comment"># =&gt; NoMethodError</span>
</pre>

<p>To opt out of the instance writer method, pass <code>instance_writer:
false</code>.</p>

<pre class="ruby"><span class="ruby-identifier">object</span>.<span class="ruby-identifier">setting</span> = <span class="ruby-keyword">false</span>  <span class="ruby-comment"># =&gt; NoMethodError</span>
</pre>

<p>To opt out of both instance methods, pass <code>instance_accessor:
false</code>.</p>
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-i-class_attribute_source')" id="l_method-i-class_attribute_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/941986c7db3f4abaed6f859a4d7b93e6445039c4/activesupport/lib/active_support/core_ext/class/attribute.rb#L71" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-i-class_attribute_source" class="dyn-source">
                <pre><span class="ruby-comment"># File activesupport/lib/active_support/core_ext/class/attribute.rb, line 71</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">class_attribute</span>(*<span class="ruby-identifier">attrs</span>)
  <span class="ruby-identifier">options</span> = <span class="ruby-identifier">attrs</span>.<span class="ruby-identifier">extract_options!</span>
  <span class="ruby-comment"># double assignment is used to avoid &quot;assigned but unused variable&quot; warning</span>
  <span class="ruby-identifier">instance_reader</span> = <span class="ruby-identifier">instance_reader</span> = <span class="ruby-identifier">options</span>.<span class="ruby-identifier">fetch</span>(<span class="ruby-value">:instance_accessor</span>, <span class="ruby-keyword">true</span>) <span class="ruby-operator">&amp;&amp;</span> <span class="ruby-identifier">options</span>.<span class="ruby-identifier">fetch</span>(<span class="ruby-value">:instance_reader</span>, <span class="ruby-keyword">true</span>)
  <span class="ruby-identifier">instance_writer</span> = <span class="ruby-identifier">options</span>.<span class="ruby-identifier">fetch</span>(<span class="ruby-value">:instance_accessor</span>, <span class="ruby-keyword">true</span>) <span class="ruby-operator">&amp;&amp;</span> <span class="ruby-identifier">options</span>.<span class="ruby-identifier">fetch</span>(<span class="ruby-value">:instance_writer</span>, <span class="ruby-keyword">true</span>)
  <span class="ruby-identifier">instance_predicate</span> = <span class="ruby-identifier">options</span>.<span class="ruby-identifier">fetch</span>(<span class="ruby-value">:instance_predicate</span>, <span class="ruby-keyword">true</span>)

  <span class="ruby-identifier">attrs</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">name</span><span class="ruby-operator">|</span>
    <span class="ruby-identifier">define_singleton_method</span>(<span class="ruby-identifier">name</span>) { <span class="ruby-keyword">nil</span> }
    <span class="ruby-identifier">define_singleton_method</span>(<span class="ruby-node">&quot;#{name}?&quot;</span>) { <span class="ruby-operator">!</span><span class="ruby-operator">!</span><span class="ruby-identifier">public_send</span>(<span class="ruby-identifier">name</span>) } <span class="ruby-keyword">if</span> <span class="ruby-identifier">instance_predicate</span>

    <span class="ruby-identifier">ivar</span> = <span class="ruby-node">&quot;@#{name}&quot;</span>

    <span class="ruby-identifier">define_singleton_method</span>(<span class="ruby-node">&quot;#{name}=&quot;</span>) <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">val</span><span class="ruby-operator">|</span>
      <span class="ruby-identifier">singleton_class</span>.<span class="ruby-identifier">class_eval</span> <span class="ruby-keyword">do</span>
        <span class="ruby-identifier">remove_possible_method</span>(<span class="ruby-identifier">name</span>)
        <span class="ruby-identifier">define_method</span>(<span class="ruby-identifier">name</span>) { <span class="ruby-identifier">val</span> }
      <span class="ruby-keyword">end</span>

      <span class="ruby-keyword">if</span> <span class="ruby-identifier">singleton_class?</span>
        <span class="ruby-identifier">class_eval</span> <span class="ruby-keyword">do</span>
          <span class="ruby-identifier">remove_possible_method</span>(<span class="ruby-identifier">name</span>)
          <span class="ruby-identifier">define_method</span>(<span class="ruby-identifier">name</span>) <span class="ruby-keyword">do</span>
            <span class="ruby-keyword">if</span> <span class="ruby-identifier">instance_variable_defined?</span> <span class="ruby-identifier">ivar</span>
              <span class="ruby-identifier">instance_variable_get</span> <span class="ruby-identifier">ivar</span>
            <span class="ruby-keyword">else</span>
              <span class="ruby-identifier">singleton_class</span>.<span class="ruby-identifier">send</span> <span class="ruby-identifier">name</span>
            <span class="ruby-keyword">end</span>
          <span class="ruby-keyword">end</span>
        <span class="ruby-keyword">end</span>
      <span class="ruby-keyword">end</span>
      <span class="ruby-identifier">val</span>
    <span class="ruby-keyword">end</span>

    <span class="ruby-keyword">if</span> <span class="ruby-identifier">instance_reader</span>
      <span class="ruby-identifier">remove_possible_method</span> <span class="ruby-identifier">name</span>
      <span class="ruby-identifier">define_method</span>(<span class="ruby-identifier">name</span>) <span class="ruby-keyword">do</span>
        <span class="ruby-keyword">if</span> <span class="ruby-identifier">instance_variable_defined?</span>(<span class="ruby-identifier">ivar</span>)
          <span class="ruby-identifier">instance_variable_get</span> <span class="ruby-identifier">ivar</span>
        <span class="ruby-keyword">else</span>
          <span class="ruby-keyword">self</span>.<span class="ruby-identifier">class</span>.<span class="ruby-identifier">public_send</span> <span class="ruby-identifier">name</span>
        <span class="ruby-keyword">end</span>
      <span class="ruby-keyword">end</span>
      <span class="ruby-identifier">define_method</span>(<span class="ruby-node">&quot;#{name}?&quot;</span>) { <span class="ruby-operator">!</span><span class="ruby-operator">!</span><span class="ruby-identifier">public_send</span>(<span class="ruby-identifier">name</span>) } <span class="ruby-keyword">if</span> <span class="ruby-identifier">instance_predicate</span>
    <span class="ruby-keyword">end</span>

    <span class="ruby-identifier">attr_writer</span> <span class="ruby-identifier">name</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">instance_writer</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
        
        <div class="method">
          <div class="title method-title" id="method-i-subclasses">
            
              <b>subclasses</b>()
            
            <a href="Class.html#method-i-subclasses" name="method-i-subclasses" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              <p>Returns an array with the direct children of <code>self</code>.</p>

<pre class="ruby"><span class="ruby-constant">Integer</span>.<span class="ruby-identifier">subclasses</span> <span class="ruby-comment"># =&gt; [Fixnum, Bignum]</span>

<span class="ruby-keyword">class</span> <span class="ruby-constant">Foo</span>; <span class="ruby-keyword">end</span>
<span class="ruby-keyword">class</span> <span class="ruby-constant">Bar</span> <span class="ruby-operator">&lt;</span> <span class="ruby-constant">Foo</span>; <span class="ruby-keyword">end</span>
<span class="ruby-keyword">class</span> <span class="ruby-constant">Baz</span> <span class="ruby-operator">&lt;</span> <span class="ruby-constant">Bar</span>; <span class="ruby-keyword">end</span>

<span class="ruby-constant">Foo</span>.<span class="ruby-identifier">subclasses</span> <span class="ruby-comment"># =&gt; [Bar]</span>
</pre>
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-i-subclasses_source')" id="l_method-i-subclasses_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/d47b90e76c989a8f0ca32c140e9b7220da0e5edc/activesupport/lib/active_support/core_ext/class/subclasses.rb#L35" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-i-subclasses_source" class="dyn-source">
                <pre><span class="ruby-comment"># File activesupport/lib/active_support/core_ext/class/subclasses.rb, line 35</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">subclasses</span>
  <span class="ruby-identifier">subclasses</span>, <span class="ruby-identifier">chain</span> = [], <span class="ruby-identifier">descendants</span>
  <span class="ruby-identifier">chain</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">k</span><span class="ruby-operator">|</span>
    <span class="ruby-identifier">subclasses</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">k</span> <span class="ruby-keyword">unless</span> <span class="ruby-identifier">chain</span>.<span class="ruby-identifier">any?</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">c</span><span class="ruby-operator">|</span> <span class="ruby-identifier">c</span> <span class="ruby-operator">&gt;</span> <span class="ruby-identifier">k</span> }
  <span class="ruby-keyword">end</span>
  <span class="ruby-identifier">subclasses</span>
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
        
        <div class="method">
          <div class="title method-title" id="method-i-superclass_delegating_accessor">
            
              <b>superclass_delegating_accessor</b>(name, options = {})
            
            <a href="Class.html#method-i-superclass_delegating_accessor" name="method-i-superclass_delegating_accessor" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-i-superclass_delegating_accessor_source')" id="l_method-i-superclass_delegating_accessor_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/f1220516fba3add0f3b491a440fd6525fdb61587/activesupport/lib/active_support/core_ext/class/delegating_attributes.rb#L5" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-i-superclass_delegating_accessor_source" class="dyn-source">
                <pre><span class="ruby-comment"># File activesupport/lib/active_support/core_ext/class/delegating_attributes.rb, line 5</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">superclass_delegating_accessor</span>(<span class="ruby-identifier">name</span>, <span class="ruby-identifier">options</span> = {})
  <span class="ruby-comment"># Create private _name and _name= methods that can still be used if the public</span>
  <span class="ruby-comment"># methods are overridden. This allows</span>
  <span class="ruby-identifier">_superclass_delegating_accessor</span>(<span class="ruby-node">&quot;_#{name}&quot;</span>)

  <span class="ruby-comment"># Generate the public methods name, name=, and name?</span>
  <span class="ruby-comment"># These methods dispatch to the private _name, and _name= methods, making them</span>
  <span class="ruby-comment"># overridable</span>
  <span class="ruby-identifier">singleton_class</span>.<span class="ruby-identifier">send</span>(<span class="ruby-value">:define_method</span>, <span class="ruby-identifier">name</span>) { <span class="ruby-identifier">send</span>(<span class="ruby-node">&quot;_#{name}&quot;</span>) }
  <span class="ruby-identifier">singleton_class</span>.<span class="ruby-identifier">send</span>(<span class="ruby-value">:define_method</span>, <span class="ruby-node">&quot;#{name}?&quot;</span>) { <span class="ruby-operator">!</span><span class="ruby-operator">!</span><span class="ruby-identifier">send</span>(<span class="ruby-node">&quot;_#{name}&quot;</span>) }
  <span class="ruby-identifier">singleton_class</span>.<span class="ruby-identifier">send</span>(<span class="ruby-value">:define_method</span>, <span class="ruby-node">&quot;#{name}=&quot;</span>) { <span class="ruby-operator">|</span><span class="ruby-identifier">value</span><span class="ruby-operator">|</span> <span class="ruby-identifier">send</span>(<span class="ruby-node">&quot;_#{name}=&quot;</span>, <span class="ruby-identifier">value</span>) }

  <span class="ruby-comment"># If an instance_reader is needed, generate methods for name and name= on the</span>
  <span class="ruby-comment"># class itself, so instances will be able to see them</span>
  <span class="ruby-identifier">define_method</span>(<span class="ruby-identifier">name</span>) { <span class="ruby-identifier">send</span>(<span class="ruby-node">&quot;_#{name}&quot;</span>) } <span class="ruby-keyword">if</span> <span class="ruby-identifier">options</span>[<span class="ruby-value">:instance_reader</span>] <span class="ruby-operator">!=</span> <span class="ruby-keyword">false</span>
  <span class="ruby-identifier">define_method</span>(<span class="ruby-node">&quot;#{name}?&quot;</span>) { <span class="ruby-operator">!</span><span class="ruby-operator">!</span><span class="ruby-identifier">send</span>(<span class="ruby-node">&quot;#{name}&quot;</span>) } <span class="ruby-keyword">if</span> <span class="ruby-identifier">options</span>[<span class="ruby-value">:instance_reader</span>] <span class="ruby-operator">!=</span> <span class="ruby-keyword">false</span>
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
                    </div>

    </div>
  </body>
</html>    